* create clasification table *


use "../firmleveltest/test_sample_with_results.dta", clear
/* creating firm list with excel format*/
order nm_firm ad_firm no_license firmID region mints1 tsrank ftt_tau1s ftt_ts1s ftt_tau1p ftt_ts1p ftt_Nl1s ftt_Nr1p 
sort mints1
drop ftt_flip1 - ftt_hr2p rank1_q - neg_ftt_ts1s tsrank
*partic,mints1のどっとを埋める
sort region firmID partic
by region firmID: replace partic = partic[_n-1] if partic[_n-1] < . & _n > 1
sort region firm mints1
by region firmID: replace mints1 = mints1[_n-1] if mints1[_n-1] < . & _n > 1

/* 242 firms */
merge m:1 firmID region using "../data/ring_list020119.dta"
drop _merge
egen regionfirmTG = tag(region firmID)
keep if regionfirmTG == 1
gsort mints1 -firmID

gen index = _n
gen Collusion = "No"
replace Collusion = "Yes" if tsrank < .
keep if index < 1144
keep index regionfirmID Collusion
order index regionfirmID Collusion

local continue = 1
local n_rows = 40
local c = 1
forvalues x = 1(1)1143{
	local i = `x' - (`c'-1)*`n_rows'
	di "x: `x'; i: `i'; "
	cap gen index`c' = "\"
	cap gen regionfirmID`c' = ""
	cap gen Collusion`c' = ""
	cap gen blank`c' = ""
	qui replace index`c' = "(" + strofreal(index[`x'])+ ")" if _n == `i'
	qui replace regionfirmID`c' = strofreal(regionfirmID[`x']) if _n == `i'
	qui replace Collusion`c' = Collusion[`x'] if _n == `i'
	if `i' == `n_rows'{
		local c = `c' + 1
	}
}
drop if _n > `n_rows'
local x = `n_rows' + 1
set obs `x'
replace index = 0 if index == .
sort index
forvalues  xc = 1(1)`c'{
	qui replace index`xc' = "" if _n == 1
	qui replace regionfirmID`xc' = "Firm ID" if _n == 1
	qui replace Collusion`xc' = "Collusion" if _n == 1
}

forvalues x = 1(1)10{
	local start = (`x'-1) * 3 + 1
	local end = min(`start' + 2, `c')
	preserve
		keep index`start' - blank`end'
		*export delimited using ../firmleveltest/classification`x'.csv, replace novarnames
		export delimited using ../tables/classification`x'.csv, replace 
		/*if `x' == 1{
			python script modules/tably.py, ///
				args(../firmleveltest/classification`x'.csv -f -ero ///
				../tables/classification`x'.tex)
		}
		else{
			python script modules/tably.py, ///
				args(../firmleveltest/classification`x'.csv -ero ///
				../tables/classification`x'.tex)			
		}
		*/
		sleep 400
		*erase ../firmleveltest/classification`x'.csv
	restore
}